/*
版权@hehung所有，转载请申明
email：1398660197@qq.com
*/
#include "app_display.h"
#include "app_oled.h"
#include "oled_drv.h"
#include "app_main.h"
#include "app_rtc.h"
#include "app_oled_bmp.h"
#include "app_alarm.h"
#include "app_player.h"
#include "app_kv.h"
#include "app_weather.h"
#include "app_iot.h"

#define DISP_DEBUG                 (STD_ON)
#define DISP_TIME_SPECIFITY        (STD_ON)
#define DISP_WIFI_DEBUG            (STD_ON)

#define DISP_OLED_FILL_BLACK()     OLED_Clear(OLED_FILL_BLACK)
#define DISP_OLED_FILL_WHITE()     OLED_Clear(OLED_FILL_WHITE)

#define DISP_WARNING_SHOW_TIME     (2000U)    /* 警告信息显示时间-2s */

/* 音量调节的周期 */
#define VOLUME_INCREASE_T            (5U)    /* 100ms */

typedef void (*DisplayFunc)(void);


static aos_task_t disp_task_h;    /* Display task handle */
static e_DispFlg disp_flg = DISP_INIT;    /* 显示界面标志 */
static uint32_t disp_task_delay_ms = 200u;    /* 显示界面线程切换时长ms */
static e_DispMenu menu_flg = DISP_MENU_SETTING;    /* 设置界面下用于标记当前选中的图标 */
static e_DispDateSetting date_setting_flg = DISP_DATE_SETTING_HOUR;    /* 日期设置界面下用于标记当前选中目标 */
static e_DispSetMenu setting_menu_flg = DISP_SETTING_MENU_ALARM;    /* 设置菜单标记 */
/* 闹钟设置相关变量 */
static e_DispAlarmSetting setting_alarm_flg = DISP_SETTING_ALARM_PREV;    /* 标记闹钟 */
static uint8_t alarm_num = 1;        /* 默认闹钟个数 */
static e_DispAlarmDetailSetting detail_setting_alarm_flg = DISP_SR_DETAIL_PREV;
static e_AlarmType alarm_time[ALARM_NUM_MAX];
static uint16_t alarm_aj_t = 0;
static uint8_t alarm_af_flg = 0;
/* 设置相关的变量 */
static uint8_t setting_volume = 60;            /* 音量调节标志 */
static uint8_t setting_voice_effect_en = 1;    /* 音效使能标志 */
static uint8_t setting_time_report_en = 1;     /* 报时使能标志 */
static uint8_t setting_ntp_en = 1;             /* NTP校时设置 */
/* 城市代码设置 */
static uint8_t weather_city_code_split[9] = {0};
static e_DispCitySetting setting_city_flag = DISP_CITY_CODE_1;
/* WIFI设置 */
static uint8_t wifi_setting_flag = 0;
#if (DISP_WIFI_DEBUG == STD_OFF)
static uint8_t wifi_ssid_name_len = 1;    /* wifi名字长度 */
static char wifi_ssid_name[32] = {[0]='0', [1 ... 31] = '\0'};    /* wifi名字 */
static uint8_t wifi_pwd_len = 8;   /* wifi密码长度 */
static char wifi_pwd[64] = {[0 ... 7]='0', [8 ... 63] = '\0'};  /* wifi密码 */
#else
static uint8_t wifi_ssid_name_len = 11;    /* wifi名字长度 */
static char wifi_ssid_name[32] = {"hehungphone"};    /* wifi名字 */
static uint8_t wifi_pwd_len = 10;   /* wifi密码长度 */
static char wifi_pwd[64] = {"1234543210"};  /* wifi密码 */
#endif
/* 表示ASCII的高位数和低位数
key_input_char[1]-高位数
key_input_char[0]-低位数 */
static uint8_t key_input_char[2] = {2, 0};
 

static void Disp_TaskEntry(void *arg);
static void Disp_MainHandler(void);
static void Disp_ShowMainGui(void);
static void Disp_ShowMenuGui(void);
static void Disp_ShowDateSettingGui(void);
static void Disp_ShowBellSettingGui(void);
static void Disp_ShowWeatherGui(void);
static void Disp_ShowSettingMenuGui(void);
static void Disp_ShowBellSettingDetailGui(void);
static void Disp_ShowDateSettingErrorGui(void);
static void Disp_FuncNull(void);
static void DISP_LoadSettingPara(void);
static void Disp_ShowCitySettingGui(void);
static void Disp_ShowWifiConfigGui(void);
static void Disp_CityCodeSplit(uint32_t citycode);
static void Disp_CityCodeMeger(void);
static void Disp_InitWifiNameOrPsd(char *arr, uint8_t len, uint8_t type);

/* 显示函数初始化 */
void Disp_Init(void)
{
	int task_s;
	
	oled_init();    /* 初始化OLED SPI */
	 
	task_s = aos_task_new_ext(&disp_task_h,
								"disp_task", 
								Disp_TaskEntry, 
								NULL, 
								2048*2, 
								5);
	if (0 == task_s)
	{
		printf("APP_OLED Task create success\r\n");
	}
	else
	{
		printf("APP_OLED Task create failed\r\n");
	}
}

/* 显示函数任务入口 */
static void Disp_TaskEntry(void *arg)
{
#if (DISP_DEBUG == STD_ON)
//	uint8_t flag = 0;
	
	OLED_ShowString(0, 0, (const uint8_t*)"T-HEAD", 16, 1);
	OLED_ShowString(0, 16, (const uint8_t*)"EEWORLD", 16, 1);
	OLED_ShowString(0, 32, (const uint8_t*)"NetworkClock", 16, 1);
	OLED_ShowString(0, 48, (const uint8_t*)"hehung", 16, 1);
	OLED_Refresh_Gram();

	disp_flg = DISP_TIME;
#endif
	// disp_flg = DISP_WIFI;
    while (1) 
	{
		DISP_LoadSettingPara();
		Disp_MainHandler();

		aos_msleep(disp_task_delay_ms);
    }
}

/* 显示界面处理主函数 */
static void Disp_MainHandler(void)
{	
	const DisplayFunc DispFunc[] = 
	{
		Disp_FuncNull,
		Disp_ShowMainGui,
		Disp_ShowMenuGui,
		Disp_ShowSettingMenuGui,
		Disp_ShowDateSettingGui,
		Disp_ShowBellSettingGui,
		Disp_ShowBellSettingDetailGui,
		Disp_ShowCitySettingGui,
		Disp_ShowWifiConfigGui,
		Disp_ShowWeatherGui,
		Disp_ShowDateSettingErrorGui
	};

	/* 清空OLED buffer，防止出现重叠字符 */
	OLED_FillZone(0, 0, 127, 63, 0);
	
	/* 调用显示函数 */
	DispFunc[disp_flg]();
}

static void DISP_LoadSettingPara(void)
{
	static uint16_t last_player_en_Flg = 0xFFFF;
	uint16_t cur_vol;
	
	cur_vol = Player_GetStartFlg();
	/* Player 初始化成功了 */
	if (last_player_en_Flg != cur_vol)
	{
		/* 获取当前音量 */
		setting_volume = cur_vol;
		setting_time_report_en = Player_GetVoiceTimeFlg();
		setting_voice_effect_en = Player_GetVoiceEffectFlg();
	}

	/* NTP 初始化成功了 */
	if (1 == Rtc_GetNtpInitFishFlg())
	{
		/* 获取NTP使能状态 */
		setting_ntp_en = Rtc_GetNtpEnStatus();
	}
	
	last_player_en_Flg = cur_vol;
}

/* 空函数 */
static void Disp_FuncNull(void)
{
	/* Do Nothing */
}

/* 主界面GUI显示函数，显示时间，日期，星期，联网状态 */
static void Disp_ShowMainGui(void)
{
	csi_rtc_time_t cur_time;
#if (DISP_TIME_SPECIFITY == STD_OFF)
	char cur_time[10];
	char date_str[16];
#else
	char time_h_str[4];
	char time_m_str[4];
	char time_s_str[4];
	char date_d_str[4];
	char date_m_str[4];
	const char *week_hz[] = {"日", "一", "二", "三", "四", "五", "六"};
	uint8_t weekday = 0;
#endif
	uint8_t cur_mon;
	uint16_t cur_year;
	uint8_t network_conn_flg;              /* 网络连接标志 */
	static uint8_t net_lg_flg = 0;
	uint8_t alarm_cnt;
	e_AlarmType alarm_en_l;
    char temp[10];
	static uint8_t weather_dis_cnt = 0;
	s_WeatherType weather;
	static uint8_t Iot_conn_flg = 0;
	
	cur_time = Rtc_GetCurrentTime();
	cur_mon  = cur_time.tm_mon + 1;
	cur_year = cur_time.tm_year + 1900;
	
	/* 格式化时间和日期 */
#if (DISP_TIME_SPECIFITY == STD_OFF)
	sprintf(time_str, "%02d:%02d:%02d", cur_time.tm_hour, cur_time.tm_min, cur_time.tm_sec);
	sprintf(date_str, "%04d-%02d-%02d", cur_year, cur_mon, cur_time.tm_mday);
	
	OLED_ShowString(16, 20, (const uint8_t*)time_str, 36, 1);
	OLED_ShowString(24, 48, (const uint8_t*)date_str, 16, 1);
#else
	/* 显示时间和日期 */
	sprintf(time_h_str, "%02d", cur_time.tm_hour);
	sprintf(time_m_str, "%02d", cur_time.tm_min);
	sprintf(time_s_str, "%02d", cur_time.tm_sec);
	OLED_ShowString(20, 14, (const uint8_t*)time_h_str, 36, 1);
	OLED_ShowChar(56, 14+8, (uint8_t)':', 16, 1);
	OLED_ShowString(64, 14, (const uint8_t*)time_m_str, 36, 1);
	OLED_ShowString(102, 14+16, (const uint8_t*)time_s_str, 16, 1);
	sprintf(date_m_str, "%02d", cur_mon);
	sprintf(date_d_str, "%02d", cur_time.tm_mday);
	OLED_ShowString(4, 48, (const uint8_t*)date_m_str, 16, 1);
	OLED_ShowHz(20, 48, (const char *)"月", 1);
	OLED_ShowString(36, 48, (const uint8_t*)date_d_str, 16, 1);
	OLED_ShowHz(52, 48, (const char *)"日", 1);
	OLED_ShowHzStringRow(76, 48, (const char *)"星期", 1 );
#if 0
	//使用RTC的星期总是不正确，所以该项目使用泰勒公式计算星期
	if(cur_time.tm_wday > 6)
		cur_time.tm_wday = 6;
	OLED_ShowHz(108, 48, (const char *)week_hz[cur_time.tm_wday], 1 ); 
#else
	//泰勒公式,当日期变化了之后重新计算当前星期
	weekday = Rtc_CalculateWeekday(cur_year, cur_mon, cur_time.tm_mday);
	OLED_ShowHz(108, 48, (const char *)week_hz[weekday], 1 ); 
#endif
#endif
	/* 显示网络连接状态 */
	network_conn_flg = netmgr_is_gotip(Net_GetnetMgrHdl());
	OLED_FillZone(0, 0, 16, 16, 0);
	if(1 == network_conn_flg)
	{
		OLED_DrawBmpImg2(0, 0, wifi_logo_16bit, WIFI_LOGO_SIZE_16BIT, 16, 1);
	}
	else
	{
		net_lg_flg ^= 1;
		if (0 == net_lg_flg)
		{
			OLED_DrawBmpImg2(0, 0, wifi_logo_16bit, WIFI_LOGO_SIZE_16BIT, 16, 1);
		}
	}

	/* 显示Iot连接状态 */
	OLED_FillZone(36, 0, 54, 16, 0);
	if (IOT_DEVICE_DISCONN != Iot_GetConnectStatus())
	{
		OLED_ShowString(36, 0, (const uint8_t*)"IoT", 12, 1);
	}
	else
	{
		Iot_conn_flg ^= 1;
		if (0 == Iot_conn_flg)
		{
			OLED_ShowString(36, 0, (const uint8_t*)"IoT", 12, 1);
		}
	}
	
	/* 显示闹钟图标 */
	OLED_FillZone(18, 0, 34, 16, 0);
	for (alarm_cnt=0; alarm_cnt<ALARM_NUM_MAX; alarm_cnt++)
	{
		alarm_en_l = Alarm_GetAlarmTime(alarm_cnt);
		/* 如果有任意一个闹钟被使能 */
		if (1 == alarm_en_l.alarm_en)
		{
			OLED_DrawBmpImg2(18, 0, bell_logo_16bit, BELL_LOGO_SIZE_16BIT, 16, 1);
			break;
		}
	}

	/* 显示天气信息 */
	if (1 == Weather_GetSuccessFlg())    /* 当成功获取到了天气 */
	{
		weather = Weather_GetWeatherInfo();
		weather_dis_cnt =  (weather_dis_cnt == 40)?(0):(weather_dis_cnt+1);

		if (weather_dis_cnt < 20)
		{
			sprintf(temp, "%02d%%|%02d", weather.humi_curr, weather.temp_curr);
			if (weather.temp_curr < 0)
			{
				OLED_ShowString(56, 0, (const uint8_t*)temp, 16, 1);
			}
			else
			{
				OLED_ShowString(64, 0, (const uint8_t*)temp, 16, 1);
			}
			OLED_ShowHz(112, 0, (const char *)"℃", 1 );
		}
		else if(weather_dis_cnt < 40)
		{
			// sprintf(temp, "%s", weather.weather_curr);
			OLED_ShowHzStringRow(64, 0, (const char *)weather.weather_curr, 1);
		}
	}
	else
	{
		OLED_ShowHzStringRow(80, 0, (const uint8_t*)"未知", 1);
	}
	
	OLED_Refresh_Gram();
}

/* 菜单选择界面 */
static void Disp_ShowMenuGui(void)
{
	uint8_t sel_flg = 0;
	const char *menu_hz[] = {"设置", "联网", "天气", "退出"};
	
	OLED_DrawBmpImg2(36, 0, menu_logo_16bit, MENU_LOGO_SIZE_16BIT, 16, 1);
	OLED_ShowHzStringRow(60, 0, "菜单", 1);
	
	OLED_DrawBmpImg2(8, 20, setting_logo_24bit, SETTING_LOGO_SIZE_24BIT, 24, !(menu_flg==DISP_MENU_SETTING));
	OLED_DrawBmpImg2(40, 20, wifi_logo_24bit, WIFI_LOGO_SIZE_24BIT, 24, !(menu_flg==DISP_MENU_WIFI_CONFIG));
	OLED_DrawBmpImg2(72, 20, sun_logo_24bit, SUN_LOGO_SIZE_24BIT, 24, !(menu_flg==DISP_MENU_WEATHER));
	OLED_DrawBmpImg2(104, 20, exit_logo_24bit, EXIT_LOGO_SIZE_24BIT, 24, !(menu_flg==DISP_MENU_EXIT));

	switch(menu_flg)
	{
		case DISP_MENU_SETTING:     sel_flg = 0; break;
		case DISP_MENU_WIFI_CONFIG: sel_flg = 1; break;
		case DISP_MENU_WEATHER:     sel_flg = 2; break;
		case DISP_MENU_EXIT:        sel_flg = 3; break;
	}
	
	OLED_ShowHzStringRow(48, 48, (const char*)menu_hz[sel_flg], 1);
	
	OLED_Refresh_Gram();
}

/* 设置菜单界面 */
static void Disp_ShowSettingMenuGui(void)
{
	/* 用于标记当前显示的设置菜单项 */
	static uint8_t dis_menu_flag = 0;
	const char *setting_menu_list[] = {
		"闹钟", "音量", "音效", "报时", "联网校时", "时间", "城市", "退出"};
	char volume_f[5];
    
	OLED_DrawBmpImg2(36, 0, setting_logo_16bit, SETTING_LOGO_SIZE_16BIT, 16, 1);
	OLED_ShowHzStringRow(60, 0, "设置", 1);
	
	if (setting_menu_flg < 3)
	{
		dis_menu_flag = 0;

		OLED_FillZone(88, 32, 88+8*4, 32+16, 0);
		/* 获取当前音量, 并转换为百分比 */
		sprintf(volume_f, "%d%%", setting_volume);
		OLED_ShowString(88, 32, (const uint8_t*)volume_f,16, 1);
		setting_voice_effect_en = Player_GetVoiceEffectFlg();
		OLED_ShowHzStringRow(88, 48, (setting_voice_effect_en == 1)?"■":"□", 1);
	}
	else if(setting_menu_flg < 6)
	{
		dis_menu_flag = 3;

		OLED_ShowHzStringRow(88, 16, (setting_time_report_en == 1)?"■":"□", 1);
		OLED_ShowHzStringRow(88, 32, (setting_ntp_en == 1)?"■":"□", 1);
	}
	else if(setting_menu_flg < 9)
	{
		dis_menu_flag = 6;
	}

	OLED_ShowChar(6, 16 + ((setting_menu_flg%3)<<4), '>',16, 1);
	OLED_ShowHzStringRow(16, 16, setting_menu_list[dis_menu_flag], 1);
	if ((dis_menu_flag + 1) <= DISP_SETTING_MENU_EXIT)    /* 存在才会显示，不然会数组溢出 */
		OLED_ShowHzStringRow(16, 32, setting_menu_list[dis_menu_flag + 1], 1);
	if ((dis_menu_flag + 2) <= DISP_SETTING_MENU_EXIT)    /* 存在才会显示，不然会数组溢出 */
		OLED_ShowHzStringRow(16, 48, setting_menu_list[dis_menu_flag + 2], 1);

	OLED_Refresh_Gram();
}

/* 时间设置界面 */
static void Disp_ShowDateSettingGui(void)
{
	csi_rtc_time_t tmp_set_time;
	
	tmp_set_time = Rtc_GetSettingTime();
	
	OLED_DrawBmpImg2(20, 0, time_logo_16bit, TIME_LOGO_SIZE_16BIT, 16, 1);
	OLED_ShowHzStringRow(44, 0, "设置时间", 1);

	OLED_ShowNum(32, 16, tmp_set_time.tm_hour, 2, 16, !(date_setting_flg==DISP_DATE_SETTING_HOUR));
	OLED_ShowChar(48, 16, ':', 16, 1);
	OLED_ShowNum(56, 16, tmp_set_time.tm_min, 2, 16, !(date_setting_flg==DISP_DATE_SETTING_MIN));
	OLED_ShowChar(74, 16, ':', 16, 1);
	OLED_ShowNum(82, 16, tmp_set_time.tm_sec, 2, 16, !(date_setting_flg==DISP_DATE_SETTING_SEC));

	OLED_ShowNum(24, 32, tmp_set_time.tm_year+1900, 4, 16, !(date_setting_flg==DISP_DATE_SETTING_YEAR));
	OLED_ShowChar(56, 32, '-', 16, 1);
	OLED_ShowNum(64, 32, tmp_set_time.tm_mon+1, 2, 16, !(date_setting_flg==DISP_DATE_SETTING_MON));
	OLED_ShowChar(80, 32, '-', 16, 1);
	OLED_ShowNum(88, 32, tmp_set_time.tm_mday, 2, 16, !(date_setting_flg==DISP_DATE_SETTING_MDAY));
	OLED_DrawBmpImg2(112, 48, exit_logo_16bit, EXIT_LOGO_SIZE_16BIT, 16, 
							!(date_setting_flg==DISP_DATE_SETTING_EXIT));
							
	OLED_Refresh_Gram();
}

/* 闹钟设置界面 */
static void Disp_ShowBellSettingGui(void)
{
	char alarm_time_str[8];
	
	OLED_DrawBmpImg2(36, 0, bell_logo_16bit, BELL_LOGO_SIZE_16BIT, 16, 1);
	OLED_ShowHzStringRow(60, 0, "闹钟", 1);
	
	OLED_ShowChar(20, 24, '<', 16, !(setting_alarm_flg==DISP_SETTING_ALARM_PREV));
	OLED_ShowChar(108, 24, '>', 16, !(setting_alarm_flg==DISP_SETTING_ALARM_NEXT));			
	
	OLED_ShowNum(32, 24, alarm_num, 2, 16, 1);
	OLED_ShowChar(48, 24, '-', 16, 1);
	/* 获取闹钟时间 */
	alarm_time[alarm_num-1] = Alarm_GetAlarmTime(alarm_num-1);
	sprintf(alarm_time_str, "%02d:%02d", alarm_time[alarm_num-1].alarm_h, alarm_time[alarm_num-1].alarm_m);
	OLED_ShowString(56, 24, (const uint8_t*)alarm_time_str, 16, !(setting_alarm_flg==DISP_SETTING_ALARM_ENTER));
	
	OLED_DrawBmpImg2(112, 48, exit_logo_16bit, EXIT_LOGO_SIZE_16BIT, 16, 
							!(setting_alarm_flg==DISP_SETTING_ALARM_EXIT));
							
	OLED_Refresh_Gram();
}

/* 闹钟细节设置界面 */
static void Disp_ShowBellSettingDetailGui(void)
{
	uint8_t x;
	uint8_t y;
	
	OLED_DrawBmpImg2(36, 0, bell_logo_16bit, BELL_LOGO_SIZE_16BIT, 16, 1);
	OLED_ShowHzStringRow(60, 0, "闹钟", 1);
	OLED_ShowNum(16, 0, alarm_num, 2, 16, 1);	
	OLED_ShowChar(0, 0, '<', 16, !(detail_setting_alarm_flg == DISP_SR_DETAIL_PREV));
	OLED_ShowChar(120, 0, '>', 16, !(detail_setting_alarm_flg == DISP_SR_DETAIL_NEXT));		
	
	/* 获取闹钟时间 */
	alarm_time[alarm_num-1] = Alarm_GetAlarmTime(alarm_num-1);
	OLED_ShowNum(20, 16, alarm_time[alarm_num-1].alarm_h, 2, 16, !(detail_setting_alarm_flg == DISP_SR_DETAIL_HOUR));
	OLED_ShowChar(36, 16, ':', 16, 1);	
	OLED_ShowNum(44, 16, alarm_time[alarm_num-1].alarm_m, 2, 16, !(detail_setting_alarm_flg == DISP_SR_DETAIL_MIN));

	OLED_ShowHz(88, 16, (alarm_time[alarm_num-1].alarm_en==1)?"开":"关", !(detail_setting_alarm_flg == DISP_SR_DETAIL_EN));	
	// OLED_ShowHzStringRow(0, 32, "周一", 1);
	OLED_ShowHz(0, 32, "一", !(detail_setting_alarm_flg == DISP_SR_DETAIL_MON));
	OLED_ShowHz(16, 32, ((alarm_time[alarm_num-1].alarm_wk_bit)&0x01)?"⊙":"○", 1);
	OLED_ShowHz(32, 32, "二", !(detail_setting_alarm_flg == DISP_SR_DETAIL_TUES));
	OLED_ShowHz(48, 32, ((alarm_time[alarm_num-1].alarm_wk_bit>>1U)&0x01)?"⊙":"○", 1);
	OLED_ShowHz(64, 32, "三", !(detail_setting_alarm_flg == DISP_SR_DETAIL_WED));
	OLED_ShowHz(80, 32, ((alarm_time[alarm_num-1].alarm_wk_bit>>2U)&0x01)?"⊙":"○", 1);
	OLED_ShowHz(96, 32, "四", !(detail_setting_alarm_flg == DISP_SR_DETAIL_THUR));
	OLED_ShowHz(112, 32, ((alarm_time[alarm_num-1].alarm_wk_bit>>3U)&0x01)?"⊙":"○", 1);
	OLED_ShowHz(0, 48, "五", !(detail_setting_alarm_flg == DISP_SR_DETAIL_FIR));
	OLED_ShowHz(16, 48, ((alarm_time[alarm_num-1].alarm_wk_bit>>4U)&0x01)?"⊙":"○", 1);
	OLED_ShowHz(32, 48, "六", !(detail_setting_alarm_flg == DISP_SR_DETAIL_SAT));
	OLED_ShowHz(48, 48, ((alarm_time[alarm_num-1].alarm_wk_bit>>5U)&0x01)?"⊙":"○", 1);
	OLED_ShowHz(64, 48, "日", !(detail_setting_alarm_flg == DISP_SR_DETAIL_SUN));
	OLED_ShowHz(80, 48, ((alarm_time[alarm_num-1].alarm_wk_bit>>6U)&0x01)?"⊙":"○", 1);
	
	OLED_DrawBmpImg2(112, 48, exit_logo_16bit, EXIT_LOGO_SIZE_16BIT, 16, 
							!(detail_setting_alarm_flg==DISP_SR_DETAIL_EXIT));

	for (x = 18; x < 62; x++)
	{
		OLED_DrawPoint(x, 16, 1);
		OLED_DrawPoint(x, 32, 1);
	}
	for (y = 16; y <= 32; y++)
	{
		OLED_DrawPoint(18, y, 1);
		OLED_DrawPoint(62, y, 1);
	}
							
	OLED_Refresh_Gram();
}

/* 城市代码设置界面
每个城市有唯一的城市天气代码如：北京 - 101010100 */
static void Disp_ShowCitySettingGui(void)
{
	OLED_DrawBmpImg2(36, 0, location_logo_16bit, MAP_LOGO_SIZE_16BIT, 16, 1);
	OLED_ShowHzStringRow(60, 0, (const uint8_t*)"城市", 1);

	OLED_ShowHzStringRow(0, 16, (const uint8_t*)"城市代码", 1);
	OLED_ShowChar(64, 16, (uint8_t)':', 16, 1);

	OLED_ShowNum(28,  32, weather_city_code_split[0], 1, 16, !(setting_city_flag==DISP_CITY_CODE_1));
	OLED_ShowNum(36,  32, weather_city_code_split[1], 1, 16, !(setting_city_flag==DISP_CITY_CODE_2));
	OLED_ShowNum(44, 32, weather_city_code_split[2], 1, 16, !(setting_city_flag==DISP_CITY_CODE_3));
	OLED_ShowNum(52, 32, weather_city_code_split[3], 1, 16, !(setting_city_flag==DISP_CITY_CODE_4));
	OLED_ShowNum(60, 32, weather_city_code_split[4], 1, 16, !(setting_city_flag==DISP_CITY_CODE_5));
	OLED_ShowNum(68, 32, weather_city_code_split[5], 1, 16, !(setting_city_flag==DISP_CITY_CODE_6));
	OLED_ShowNum(76, 32, weather_city_code_split[6], 1, 16, !(setting_city_flag==DISP_CITY_CODE_7));
	OLED_ShowNum(84, 32, weather_city_code_split[7], 1, 16, !(setting_city_flag==DISP_CITY_CODE_8));
	OLED_ShowNum(92, 32, weather_city_code_split[8], 1, 16, !(setting_city_flag==DISP_CITY_CODE_9));

	OLED_DrawBmpImg2(112, 48, exit_logo_16bit, EXIT_LOGO_SIZE_16BIT, 16, 
							!(setting_city_flag==DISP_CITY_EXIT));

	OLED_Refresh_Gram();
}

/* 合并每一位天气代码 */
static void Disp_CityCodeMeger(void)
{
	uint8_t i = 0;
	uint32_t weather_city_code = 0;

	weather_city_code = weather_city_code_split[0];
	for (i = 1; i < 9; i++)
	{
		weather_city_code *= 10;
		weather_city_code += weather_city_code_split[i];
	}
	// printf("\n%d\n", weather_city_code);
	Weather_SettingCityCode(weather_city_code);
}

/* 计算天气代码的每一位 */
static void Disp_CityCodeSplit(uint32_t citycode)
{
	uint32_t weather_city_code = 0;
	uint8_t i = 0;
	uint32_t muti = 100000000;

	weather_city_code = citycode; 
	for (i = 0; i < 9; i++)
	{
		weather_city_code_split[i] = weather_city_code/muti;
		weather_city_code %= muti;
		muti /= 10;
	}
}

/* wifi配置界面 */
static void Disp_ShowWifiConfigGui(void)
{
	uint8_t i = 0;
	uint16_t pos_x = 0;
	uint8_t pos_y = 0;
	uint8_t temp = 0;

	OLED_FillZone(0, 0, 128, 64, 0);

	OLED_DrawBmpImg2(36, 0, wifi_logo_16bit, WIFI_LOGO_SIZE_16BIT, 16, 1);
	OLED_ShowString(60, 0, (const uint8_t*)"WIFI", 16, 1);
	
	/* 显示按键转换后的字符串，在屏幕左上角 */
	// OLED_ShowChar(0, 0, (uint8_t)(key_input_char[1]<<4|key_input_char[0]), 16, 1);
	if ((wifi_setting_flag != 0) && 
		(wifi_setting_flag != (wifi_ssid_name_len+1)) &&
		(wifi_setting_flag != (wifi_ssid_name_len+1+wifi_pwd_len+1)))
	{
		/* 显示按键真实值，在屏幕右上角 */
		if (key_input_char[1] < 10)
		{
			/* 显示数字字符 */
			OLED_ShowChar(0, 0, (uint8_t)(key_input_char[1]+48), 16, 1);
		}
		else
		{
			/* 显示十六进制字符 */
			OLED_ShowChar(0, 0, (uint8_t)(key_input_char[1]+(65-10)), 16, 1);
		}
		if (key_input_char[0] < 10)
		{
			/* 显示数字字符 */
			OLED_ShowChar(8, 0, (uint8_t)(key_input_char[0]+48), 16, 1);
		}
		else
		{
			/* 显示十六进制字符 */
			OLED_ShowChar(8, 0, (uint8_t)(key_input_char[0]+(65-10)), 16, 1);
		}
	}

	temp = (wifi_pwd_len <= 32)?(wifi_pwd_len):(32);
	if (wifi_setting_flag <= wifi_ssid_name_len)
	{
		/* 显示wifi名字长度已经名字设置 */
		OLED_ShowHzStringRow(0, 16, "名字长度", 1);
		OLED_ShowChar(64, 16, (uint8_t)':', 16, 1);		
		OLED_ShowNum(72, 16, wifi_ssid_name_len, 2, 16, !(wifi_setting_flag == 0));

		OLED_ShowHzStringRow(88, 16, "名字", 1);
		OLED_ShowChar(120, 16, (uint8_t)':', 16, 1);		
		
		wifi_ssid_name[wifi_setting_flag-1] = key_input_char[1]<<4|key_input_char[0];
		for (i=0; i <wifi_ssid_name_len ; i++)
		{
			pos_y = (i<16)?(32):(48);
			pos_x = (i<16)?(i*8):((i-16)*8);
			OLED_ShowChar(pos_x, pos_y, (uint8_t)wifi_ssid_name[i], 16, !(wifi_setting_flag == (i+1)));
		}
	}
	else if (wifi_setting_flag <= (wifi_ssid_name_len+1+temp))
	{
		/* 显示wifi密码长度已经密码设置 */
		OLED_ShowHzStringRow(0, 16, "密码长度", 1);
		OLED_ShowChar(64, 16, (uint8_t)':', 16, 1);		
		OLED_ShowNum(72, 16, wifi_pwd_len, 2, 16, !(wifi_setting_flag == wifi_ssid_name_len+1));

		OLED_ShowHzStringRow(88, 16, "密码", 1);
		OLED_ShowChar(120, 16, (uint8_t)':', 16, 1);		
		
		wifi_pwd[wifi_setting_flag-wifi_ssid_name_len-2] = key_input_char[1]<<4|key_input_char[0];
		for (i=0; i<temp; i++)
		{
			pos_y = (i<16)?(32):(48);
			pos_x = (i<16)?(i*8):((i-16)*8);
			OLED_ShowChar(pos_x, pos_y, (uint8_t)wifi_pwd[i], 16, !(wifi_setting_flag == (i+wifi_ssid_name_len+2)));
		}
	}
	else
	{
		if (wifi_setting_flag <= (wifi_ssid_name_len+1+wifi_pwd_len))
		{
			wifi_pwd[wifi_setting_flag-wifi_ssid_name_len-2] = key_input_char[1]<<4|key_input_char[0];
		}
		
		for (i=32; i<wifi_pwd_len; i++)
		{
			pos_y = (i<48)?(16):(32);
			pos_x = (i<48)?((i-32)*8):((i-48)*8);
			OLED_ShowChar(pos_x, pos_y, (uint8_t)wifi_pwd[i], 16, !(wifi_setting_flag == (i+wifi_ssid_name_len+2)));
		}
		OLED_DrawBmpImg2(112, 48, exit_logo_16bit, EXIT_LOGO_SIZE_16BIT, 16, 
					!(wifi_setting_flag==(wifi_ssid_name_len+wifi_pwd_len+2)));
	}
	OLED_Refresh_Gram();
}

/* 初始化wifi名字或者密码数组 */
static void Disp_InitWifiNameOrPsd(char *arr, uint8_t len, uint8_t type)
{
	uint8_t i = 0;
	uint8_t max_num[2] = {32, 64};

	for (i=0; i<max_num[type]; i++)
	{
		if (i<len)
		{
			if (arr[i] == '\0')
                arr[i] = '0';
		}
		else
		{
			/* 字符串中未使用的部分都清空 */
			arr[i] = '\0'; 
		}
	}
}

/* 天气界面 */
static void Disp_ShowWeatherGui(void)
{
	static int roll_x = 0;
	static int roll_y = 16;
	s_WeatherType weather;
	char temp[10];
	char temp_curr[6];
	char humi_curr[6];

	if (1 == Weather_GetSuccessFlg())    /* 当成功获取到了天气 */
	{
		weather = Weather_GetWeatherInfo();

		if ((roll_y >= 0) && (roll_y <= 48))
		{
			OLED_ShowHzStringRow(roll_x, roll_y, (const char *)"今日", 1);
			OLED_ShowChar(roll_x + 32, roll_y, (uint8_t)':', 16, 1);
		}

		if (((roll_y+16) >= 0) && ((roll_y+16) <= 48))
		{	
			OLED_ShowChar(roll_x, roll_y + 16, (uint8_t)'-', 16, 1);
			OLED_ShowHzStringRow(roll_x + 8, roll_y + 16, (const char *)weather.weather_today, 1);
		}	

		if (((roll_y+32) >= 0) && ((roll_y+32) <= 48))
		{
			OLED_ShowChar(roll_x, roll_y + 32, (uint8_t)'-', 16, 1);
			OLED_ShowHzStringRow(roll_x + 8, roll_y + 32, (const char *)"温度", 1);
			OLED_ShowChar(roll_x + 40, roll_y + 32, (uint8_t)':', 16, 1);
			sprintf(temp, "%02d->%02d", weather.temp_low, weather.temp_high);
			OLED_ShowString(roll_x + 48, roll_y + 32, (const uint8_t*)temp, 16, 1);
			OLED_ShowHzStringRow(roll_x + 48 + strlen(temp)*8, roll_y + 32, (const char *)"℃", 1);
		}

		if (((roll_y+48) >= 0) && ((roll_y+48) <= 48))
		{
			OLED_ShowHzStringRow(roll_x, roll_y + 48, (const char *)"当前", 1);
			OLED_ShowChar(roll_x + 32, roll_y + 48, (uint8_t)':', 16, 1);
		}

		if (((roll_y+64) >= 0) && ((roll_y+64) <= 48))
		{
			OLED_ShowChar(roll_x, roll_y + 64, (uint8_t)'-', 16, 1);
			OLED_ShowHzStringRow(roll_x + 8, roll_y + 64, (const char *)weather.weather_curr, 1);
		}

		if (((roll_y+80) >= 0) && ((roll_y+80) <= 48))
		{
			OLED_ShowChar(roll_x, roll_y + 80, (uint8_t)'-', 16, 1);
			OLED_ShowHzStringRow(roll_x + 8, roll_y + 80, (const char *)"温度", 1);
			OLED_ShowChar(roll_x + 40, roll_y + 80, (uint8_t)':', 16, 1);
			sprintf(temp_curr, "%02d", weather.temp_curr);	
			OLED_ShowString(roll_x + 48, roll_y + 80, (const uint8_t*)temp_curr, 16, 1);
			OLED_ShowHzStringRow(roll_x + 48 + strlen(temp_curr)*8, roll_y + 80, (const char *)"℃", 1);
		}

		if (((roll_y+96) >= 0) && ((roll_y+96) <= 48))
		{
			OLED_ShowChar(roll_x, roll_y + 96, (uint8_t)'-', 16, 1);
			OLED_ShowHzStringRow(roll_x + 8, roll_y + 96, (const char *)"湿度", 1);
			OLED_ShowChar(roll_x + 40, roll_y + 96, (uint8_t)':', 16, 1);
			sprintf(humi_curr, "%d%%", weather.humi_curr);
			OLED_ShowString(roll_x + 48, roll_y + 96, (const uint8_t*)humi_curr, 16, 1);
		}

        roll_y = (roll_y <= -96)?(48):(roll_y-1);
	}
	else
	{
		OLED_ShowHzStringRow(32, 28, (const char *)"未知天气", 1);
	}
	OLED_FillZone(0, 0, 127, 16, 0);
	/* 写在这后面防止被之前的滚动信息覆盖，所以不能移动这句话到前面去 */
	OLED_DrawBmpImg2(36, 0, sun_logo_16bit, SUN_LOGO_SIZE_16BIT, 16, 1);
	OLED_ShowHzStringRow(60, 0, (const char *)"天气", 1);

	OLED_Refresh_Gram();
}

/* 显示手动时间错误界面
	当使能NTP设置为ON的时候才有效
 */
static void Disp_ShowDateSettingErrorGui(void)
{
	static uint16_t show_time = 0;

	if (show_time > (uint16_t)(DISP_WARNING_SHOW_TIME/disp_task_delay_ms))
	{
		show_time = 0;
		/* 退出当前界面，返回设置界面 */
		disp_flg = DISP_SETTING_MENU;
		OLED_FillZone(0, 0, 127, 63, 0);
	}
	else
	{
		show_time ++;
	}

	if (1 == show_time)
	{
		OLED_ShowString(28, 24, (const uint8_t*)"NTP", 16, 1);
		OLED_ShowHzStringRow(52, 24, "已启用", 1);

		OLED_Refresh_Gram();
	}
}

/* 按键1短按判断逻辑 */
void Disp_Key1ShortPressJudge(void)
{
	switch(disp_flg)
	{
		case DISP_TIME:
		{
			disp_flg = DISP_MENU;
			break;
		}
		case DISP_MENU:
		{
			switch(menu_flg)
			{
				case DISP_MENU_SETTING:     menu_flg = DISP_MENU_WIFI_CONFIG; break;
				case DISP_MENU_WIFI_CONFIG: menu_flg = DISP_MENU_WEATHER; break;
				case DISP_MENU_WEATHER:     menu_flg = DISP_MENU_EXIT; break;
				case DISP_MENU_EXIT:        menu_flg = DISP_MENU_SETTING; break;
			}
			break;
		}
		case DISP_SETTING_MENU:
		{
			switch(setting_menu_flg)
			{		 
				case DISP_SETTING_MENU_ALARM: setting_menu_flg++; OLED_FillZone(0, 0, 127, 63, 0); break;
				case DISP_SETTING_MENU_VOLU:  setting_menu_flg++; OLED_FillZone(0, 0, 127, 63, 0); break;
				case DISP_SETTING_MENU_VS:    setting_menu_flg++; OLED_FillZone(0, 0, 127, 63, 0); break;
				case DISP_SETTING_MENU_VTR:   setting_menu_flg++; OLED_FillZone(0, 0, 127, 63, 0); break;
				case DISP_SETTING_MENU_NTP:   setting_menu_flg++; OLED_FillZone(0, 0, 127, 63, 0); break;
				case DISP_SETTING_MENU_DATE:  setting_menu_flg++; OLED_FillZone(0, 0, 127, 63, 0); break;
				case DISP_SETTING_MENU_CITY:  setting_menu_flg++; OLED_FillZone(0, 0, 127, 63, 0); break;
				case DISP_SETTING_MENU_EXIT:  setting_menu_flg = DISP_SETTING_MENU_ALARM; 
				                              OLED_FillZone(0, 0, 127, 63, 0); 
											  break;
			}
			break;
		}
		case DISP_SETTING_ALARM:
		{
			OLED_FillZone(0, 0, 127, 63, 0);
			if(setting_alarm_flg >= DISP_SETTING_ALARM_EXIT)
			{
				setting_alarm_flg = DISP_SETTING_ALARM_PREV;
			}
			else
			{
				setting_alarm_flg++;
			}
			break;
		}
		case DISP_SETTING_ALARM_DETAIL:
		{
			OLED_FillZone(0, 0, 127, 63, 0);
			if(detail_setting_alarm_flg >= DISP_SR_DETAIL_EXIT)
			{
				detail_setting_alarm_flg = DISP_SR_DETAIL_PREV;
			}
			else
			{
				detail_setting_alarm_flg++;
			}
			break;
		}
		case DISP_SETTING_DATE:
		{
			switch(date_setting_flg)
			{	
				case DISP_DATE_SETTING_HOUR: Rtc_Setting(RTC_SETTING_HOUR, RTC_SETTING_UP); break;
				case DISP_DATE_SETTING_MIN:  Rtc_Setting(RTC_SETTING_MIN, RTC_SETTING_UP); break;
				case DISP_DATE_SETTING_SEC:  Rtc_Setting(RTC_SETTING_SEC, RTC_SETTING_UP); break;
				case DISP_DATE_SETTING_YEAR: Rtc_Setting(RTC_SETTING_YEAR, RTC_SETTING_UP); break;
				case DISP_DATE_SETTING_MON:  Rtc_Setting(RTC_SETTING_MON, RTC_SETTING_UP); break;
				case DISP_DATE_SETTING_MDAY: Rtc_Setting(RTC_SETTING_MDAY, RTC_SETTING_UP); break;
				case DISP_DATE_SETTING_EXIT: disp_flg = DISP_SETTING_MENU; Rtc_UpdateTimeManual(); break;
			}
			break;
		}
		case DISP_WIFI:
		{
			if (0 == wifi_setting_flag)
			{
				wifi_ssid_name_len++;
				if (wifi_ssid_name_len > 32)
				{
					wifi_ssid_name_len = 1;
				}
			}
			else if ((wifi_ssid_name_len+1) == wifi_setting_flag)
			{
				wifi_pwd_len = (wifi_pwd_len==0)?(8):(wifi_pwd_len+1);
				if (wifi_pwd_len > 64)
				{
					wifi_pwd_len = 0;
				}
			}
			else if ((wifi_ssid_name_len+wifi_pwd_len+2) == wifi_setting_flag)
			{
				/* 退出 */
				disp_flg = DISP_MENU;
				Wifi_SaveData2Kv(wifi_ssid_name, wifi_ssid_name_len, wifi_pwd, wifi_pwd_len);
			}
			else
			{
				key_input_char[0] = (key_input_char[0]>=0x0F)?(0x00):(key_input_char[0]+1);
				/* ASCII 可打印字符为32-126 */
				if (((key_input_char[1]<<4|key_input_char[0]) > 126))
				{
					key_input_char[0] = 0;
				}
			}

			if (wifi_setting_flag == 0)
			{
				Disp_InitWifiNameOrPsd(&wifi_ssid_name[0], wifi_ssid_name_len, 0);
			}	
			else if (wifi_setting_flag == (wifi_ssid_name_len+1))
			{
				Disp_InitWifiNameOrPsd(&wifi_pwd[0], wifi_pwd_len, 1);
			}

			break;
		}
		case DISP_WEATHER:
		{
			disp_flg = DISP_MENU;
			break;
		}
		case DISP_SETTING_CITY:
		{
			if (DISP_CITY_EXIT != setting_city_flag)
			{
				weather_city_code_split[setting_city_flag-1] = (weather_city_code_split[setting_city_flag-1] >= 9) ? 
				                                                0 : weather_city_code_split[setting_city_flag-1] + 1;
			}
			else
			{
				Disp_CityCodeMeger();
				disp_flg = DISP_SETTING_MENU;
			}
			break;
		}
		default:
		{
			break;
		}
	}
}

/* 按键2短按判断逻辑 */
void Disp_Key2ShortPressJudge(void)
{
	switch(disp_flg)
	{
		case DISP_TIME:
		{
			
			break;
		}
		case DISP_MENU:
		{
//			disp_flg = DISP_TIME;
			switch(menu_flg)
			{
				case DISP_MENU_SETTING:     menu_flg = DISP_MENU_EXIT; break;
				case DISP_MENU_WIFI_CONFIG: menu_flg = DISP_MENU_SETTING; break;
				case DISP_MENU_WEATHER:     menu_flg = DISP_MENU_WIFI_CONFIG; break;
				case DISP_MENU_EXIT:        menu_flg = DISP_MENU_WEATHER; break;
			}
			break;
		}
		case DISP_SETTING_MENU:
		{
			switch(setting_menu_flg)
			{		 
				case DISP_SETTING_MENU_ALARM: setting_menu_flg = DISP_SETTING_MENU_EXIT; 
				                              OLED_FillZone(0, 0, 127, 63, 0); 
											  break;
				case DISP_SETTING_MENU_VOLU:  setting_menu_flg--; OLED_FillZone(0, 0, 127, 63, 0); break;
				case DISP_SETTING_MENU_VS:    setting_menu_flg--; OLED_FillZone(0, 0, 127, 63, 0); break;
				case DISP_SETTING_MENU_VTR:   setting_menu_flg--; OLED_FillZone(0, 0, 127, 63, 0); break;
				case DISP_SETTING_MENU_NTP:   setting_menu_flg--; OLED_FillZone(0, 0, 127, 63, 0); break;
				case DISP_SETTING_MENU_DATE:  setting_menu_flg--; OLED_FillZone(0, 0, 127, 63, 0); break;
				case DISP_SETTING_MENU_CITY:  setting_menu_flg--; OLED_FillZone(0, 0, 127, 63, 0); break;
				case DISP_SETTING_MENU_EXIT:  setting_menu_flg--; OLED_FillZone(0, 0, 127, 63, 0); break;
			}
			break;
		}
		case DISP_SETTING_ALARM:
		{
			OLED_FillZone(0, 0, 127, 63, 0);
			if(setting_alarm_flg <= DISP_SETTING_ALARM_PREV)
			{
				setting_alarm_flg = DISP_SETTING_ALARM_EXIT;
			}
			else
			{
				setting_alarm_flg--;
			}
			break;
		}
		case DISP_SETTING_ALARM_DETAIL:
		{
			OLED_FillZone(0, 0, 127, 63, 0);
			if(detail_setting_alarm_flg <= DISP_SR_DETAIL_PREV)
			{
				detail_setting_alarm_flg = DISP_SR_DETAIL_EXIT;
			}
			else
			{
				detail_setting_alarm_flg--;
			}
			break;
		}
		case DISP_SETTING_DATE:
		{
			switch(date_setting_flg)
			{	 
				case DISP_DATE_SETTING_HOUR: Rtc_Setting(RTC_SETTING_HOUR, RTC_SETTING_DOWN);break;
				case DISP_DATE_SETTING_MIN:  Rtc_Setting(RTC_SETTING_MIN, RTC_SETTING_DOWN);break;
				case DISP_DATE_SETTING_SEC:  Rtc_Setting(RTC_SETTING_SEC, RTC_SETTING_DOWN);break;
				case DISP_DATE_SETTING_YEAR: Rtc_Setting(RTC_SETTING_YEAR, RTC_SETTING_DOWN);break;
				case DISP_DATE_SETTING_MON:  Rtc_Setting(RTC_SETTING_MON, RTC_SETTING_DOWN);break;
				case DISP_DATE_SETTING_MDAY: Rtc_Setting(RTC_SETTING_MDAY, RTC_SETTING_DOWN);break;
				case DISP_DATE_SETTING_EXIT: disp_flg = DISP_SETTING_MENU; Rtc_UpdateTimeManual(); break;
			}
			break;
		}
		case DISP_WIFI:
		{
			if (0 == wifi_setting_flag)
			{
				wifi_ssid_name_len--;
				if (wifi_ssid_name_len < 1)
				{
					wifi_ssid_name_len = 32;
				}
			}
			else if ((wifi_ssid_name_len+1) == wifi_setting_flag)
			{
				if (wifi_pwd_len == 0)
				{
					wifi_pwd_len = 64;
				}
				else
				{
					wifi_pwd_len = (wifi_pwd_len==8)?(0):(wifi_pwd_len-1);
				}
				
			}
			else if ((wifi_ssid_name_len+wifi_pwd_len+2) == wifi_setting_flag)
			{
				/* 退出 */
				disp_flg = DISP_MENU;
				Wifi_SaveData2Kv(wifi_ssid_name, wifi_ssid_name_len, wifi_pwd, wifi_pwd_len);
			}
			else
			{
				key_input_char[1] = (key_input_char[1]>=0x0F)?(0x00):(key_input_char[1]+1);
				/* ASCII 可打印字符为32-126 */
				if (((key_input_char[1]<<4|key_input_char[0]) > 126))
				{
					key_input_char[1] = 2;
				}
			}

			if (wifi_setting_flag == 0)
			{
				Disp_InitWifiNameOrPsd(&wifi_ssid_name[0], wifi_ssid_name_len, 0);
			}	
			else if (wifi_setting_flag == (wifi_ssid_name_len+1))
			{
				Disp_InitWifiNameOrPsd(&wifi_pwd[0], wifi_pwd_len, 1);
			}
			break;
		}
		case DISP_WEATHER:
		{
			disp_flg = DISP_MENU;
			break;
		}
		case DISP_SETTING_CITY:
		{
			if (DISP_CITY_EXIT != setting_city_flag)
			{
				weather_city_code_split[setting_city_flag-1] = (weather_city_code_split[setting_city_flag-1] <= 0) ? 
				                                                9 : weather_city_code_split[setting_city_flag-1] - 1;
			}
			else
			{
				Disp_CityCodeMeger();
				disp_flg = DISP_SETTING_MENU;
			}
			break;
		}
		default:
		{
			break;
		}
	}
}

/* 按键1长按判断逻辑 */
void Disp_Key1LongPressJudge(void)
{
	switch(disp_flg)
	{
		case DISP_TIME:
		{

			break;
		}
		case DISP_MENU:
		{
//			disp_flg = DISP_TIME;
			switch(menu_flg)
			{
				case DISP_MENU_SETTING:     disp_flg = DISP_SETTING_MENU; break;
				case DISP_MENU_WIFI_CONFIG: disp_flg = DISP_WIFI; 
				                            Wifi_SetConfigInfo(wifi_ssid_name, &wifi_ssid_name_len, wifi_pwd, &wifi_pwd_len); 
											break;
				case DISP_MENU_WEATHER:     disp_flg = DISP_WEATHER; break;
				case DISP_MENU_EXIT:        disp_flg = DISP_TIME;break;
			}
			break;
		}
		case DISP_SETTING_MENU:
		{
			switch(setting_menu_flg)
			{		 
				case DISP_SETTING_MENU_ALARM: disp_flg = DISP_SETTING_ALARM; break;
				case DISP_SETTING_MENU_VOLU:  Player_SetVol(setting_volume); Player_SetVoiceSettingFlg(); break;
				case DISP_SETTING_MENU_VS:    setting_voice_effect_en = !setting_voice_effect_en; Player_SetVoiceEffectFlg(setting_voice_effect_en); break;
				case DISP_SETTING_MENU_VTR:   setting_time_report_en = !setting_time_report_en; Player_SetVoiceTimeFlg(setting_time_report_en); break;
				case DISP_SETTING_MENU_NTP:   setting_ntp_en = !setting_ntp_en; Rtc_SetNtpEnStatus(setting_ntp_en); break;
				case DISP_SETTING_MENU_DATE:  
					/* 手动设置时间只能在NTP设置为OFF时才有效 */
					disp_flg = (0 == setting_ntp_en) ? (DISP_SETTING_DATE) : (DISP_SETTING_DATE_ERROR); 
					if (0 == setting_ntp_en)
						Rtc_UpdateSetFromCurTime();
					break;
				case DISP_SETTING_MENU_CITY:  disp_flg = DISP_SETTING_CITY; Disp_CityCodeSplit(Weather_GetCityCode()); break;
				case DISP_SETTING_MENU_EXIT:  disp_flg = DISP_MENU; break;
			}
			break;
		}
		case DISP_SETTING_ALARM:
		{
			switch(setting_alarm_flg)
			{
				case DISP_SETTING_ALARM_PREV: alarm_num = (alarm_num<=1)?(DISP_SETTING_ALARM_MAX):alarm_num-1; break;
				case DISP_SETTING_ALARM_ENTER: disp_flg = DISP_SETTING_ALARM_DETAIL; break;
				case DISP_SETTING_ALARM_NEXT: alarm_num = (alarm_num>=DISP_SETTING_ALARM_MAX)?(1):alarm_num+1; break;
				case DISP_SETTING_ALARM_EXIT: disp_flg =DISP_SETTING_MENU; break;
			}
			break;
		}
		case DISP_SETTING_ALARM_DETAIL:
		{
			switch(detail_setting_alarm_flg)
			{
				case DISP_SR_DETAIL_PREV: alarm_num = (alarm_num<=1)?(DISP_SETTING_ALARM_MAX):alarm_num-1; break;
				case DISP_SR_DETAIL_NEXT: alarm_num = (alarm_num>=DISP_SETTING_ALARM_MAX)?(1):alarm_num+1; break;
				case DISP_SR_DETAIL_HOUR: 
				{
					if(alarm_af_flg == 1)
					{
						alarm_af_flg = 0;
					}
					else
					{
						alarm_time[alarm_num-1].alarm_h = (alarm_time[alarm_num-1].alarm_h>=23)?0:alarm_time[alarm_num-1].alarm_h+1;
					}
					alarm_aj_t = 0;
					break;
				}
				case DISP_SR_DETAIL_MIN: 
				{
					if(alarm_af_flg == 1)
					{
						alarm_af_flg = 0;
					}
					else
					{
						alarm_time[alarm_num-1].alarm_m = (alarm_time[alarm_num-1].alarm_m>=59)?0:alarm_time[alarm_num-1].alarm_m+1;
					}	
					alarm_aj_t = 0;	
					break;			
				}
				case DISP_SR_DETAIL_EN: alarm_time[alarm_num-1].alarm_en = !alarm_time[alarm_num-1].alarm_en; break;
				case DISP_SR_DETAIL_MON: alarm_time[alarm_num-1].alarm_wk_bit = (alarm_time[alarm_num-1].alarm_wk_bit&ALARM_MONDAY_EN)?
				                                                   (alarm_time[alarm_num-1].alarm_wk_bit&(~ALARM_MONDAY_EN)):(alarm_time[alarm_num-1].alarm_wk_bit|(ALARM_MONDAY_EN)); break;
				case DISP_SR_DETAIL_TUES: alarm_time[alarm_num-1].alarm_wk_bit = (alarm_time[alarm_num-1].alarm_wk_bit&ALARM_TUESDAY_EN)?
				                                                   (alarm_time[alarm_num-1].alarm_wk_bit&(~ALARM_TUESDAY_EN)):(alarm_time[alarm_num-1].alarm_wk_bit|(ALARM_TUESDAY_EN)); break;
				case DISP_SR_DETAIL_WED: alarm_time[alarm_num-1].alarm_wk_bit = (alarm_time[alarm_num-1].alarm_wk_bit&ALARM_WEDNESDAY_EN)?
				                                                   (alarm_time[alarm_num-1].alarm_wk_bit&(~ALARM_WEDNESDAY_EN)):(alarm_time[alarm_num-1].alarm_wk_bit|(ALARM_WEDNESDAY_EN)); break;
				case DISP_SR_DETAIL_THUR: alarm_time[alarm_num-1].alarm_wk_bit = (alarm_time[alarm_num-1].alarm_wk_bit&ALARM_THURSDAY_EN)?
				                                                   (alarm_time[alarm_num-1].alarm_wk_bit&(~ALARM_THURSDAY_EN)):(alarm_time[alarm_num-1].alarm_wk_bit|(ALARM_THURSDAY_EN)); break;
				case DISP_SR_DETAIL_FIR: alarm_time[alarm_num-1].alarm_wk_bit = (alarm_time[alarm_num-1].alarm_wk_bit&ALARM_FIRDAY_EN)?
				                                                   (alarm_time[alarm_num-1].alarm_wk_bit&(~ALARM_FIRDAY_EN)):(alarm_time[alarm_num-1].alarm_wk_bit|(ALARM_FIRDAY_EN)); break;
				case DISP_SR_DETAIL_SAT: alarm_time[alarm_num-1].alarm_wk_bit = (alarm_time[alarm_num-1].alarm_wk_bit&ALARM_SATURDAY_EN)?
				                                                   (alarm_time[alarm_num-1].alarm_wk_bit&(~ALARM_SATURDAY_EN)):(alarm_time[alarm_num-1].alarm_wk_bit|(ALARM_SATURDAY_EN)); break;
				case DISP_SR_DETAIL_SUN: alarm_time[alarm_num-1].alarm_wk_bit = (alarm_time[alarm_num-1].alarm_wk_bit&ALARM_SUNDAY_EN)?
				                                                   (alarm_time[alarm_num-1].alarm_wk_bit&(~ALARM_SUNDAY_EN)):(alarm_time[alarm_num-1].alarm_wk_bit|(ALARM_SUNDAY_EN)); break;
				case DISP_SR_DETAIL_EXIT: disp_flg = DISP_SETTING_ALARM; break;
			}
			Alarm_SetAlarmTime(alarm_num-1, alarm_time[alarm_num-1]);
			break;
		}
		case DISP_SETTING_DATE:
		{
			switch(date_setting_flg)
			{	 
				case DISP_DATE_SETTING_HOUR: date_setting_flg = DISP_DATE_SETTING_MIN;break;
				case DISP_DATE_SETTING_MIN:  date_setting_flg = DISP_DATE_SETTING_SEC;break;
				case DISP_DATE_SETTING_SEC:  date_setting_flg = DISP_DATE_SETTING_YEAR;break;
				case DISP_DATE_SETTING_YEAR: date_setting_flg = DISP_DATE_SETTING_MON;break;
				case DISP_DATE_SETTING_MON:  date_setting_flg = DISP_DATE_SETTING_MDAY;break;
				case DISP_DATE_SETTING_MDAY: date_setting_flg = DISP_DATE_SETTING_EXIT;break;
				case DISP_DATE_SETTING_EXIT: date_setting_flg = DISP_DATE_SETTING_HOUR;break;
			}
			break;
		}
		case DISP_WIFI:
		{
			if (wifi_setting_flag >= (wifi_ssid_name_len+wifi_pwd_len+2))
			{
				wifi_setting_flag = 0;
			}
			else
			{
				wifi_setting_flag ++; 
			}

			if ((0 == wifi_setting_flag) || ((wifi_ssid_name_len+1) == wifi_setting_flag))
			{
				/* Do Nothing */
			}
			else if (wifi_setting_flag <= wifi_ssid_name_len)
			{
				key_input_char[0] = wifi_ssid_name[wifi_setting_flag-1]&0x0F;
				key_input_char[1] = wifi_ssid_name[wifi_setting_flag-1]>>4;
			}
			else if (wifi_setting_flag <= (wifi_ssid_name_len+1+wifi_pwd_len))
			{
				key_input_char[0] = wifi_pwd[wifi_setting_flag-wifi_ssid_name_len-2]&0x0F;
				key_input_char[1] = wifi_pwd[wifi_setting_flag-wifi_ssid_name_len-2]>>4;
			}
			break;
		}
		case DISP_WEATHER:
		{
			disp_flg = DISP_MENU;
			break;
		}
		case DISP_SETTING_CITY:
		{
			setting_city_flag = (setting_city_flag >= DISP_CITY_EXIT) ? (DISP_CITY_CODE_1):(setting_city_flag+1);
			break;
		}
		default:
		{
			break;
		}
	}
}

/* 按键2长按判断逻辑 */
void Disp_Key2LongPressJudge(void)
{
	switch(disp_flg)
	{
		case DISP_TIME:
		{
			
			break;
		}
		case DISP_MENU:
		{
			// disp_flg = DISP_TIME;
			switch(menu_flg)
			{
				case DISP_MENU_SETTING:     disp_flg = DISP_SETTING_MENU; break;
				case DISP_MENU_WIFI_CONFIG: disp_flg = DISP_WIFI; 
				                            Wifi_SetConfigInfo(wifi_ssid_name, &wifi_ssid_name_len, wifi_pwd, &wifi_pwd_len);
											break;
				case DISP_MENU_WEATHER:     disp_flg = DISP_WEATHER; break;
				case DISP_MENU_EXIT:        disp_flg = DISP_TIME;break;
			}
			break;
		}
		case DISP_SETTING_MENU:
		{
			switch(setting_menu_flg)
			{		 
				case DISP_SETTING_MENU_ALARM: disp_flg = DISP_SETTING_ALARM; break;
				case DISP_SETTING_MENU_VOLU:  Player_SetVol(setting_volume); Player_SetVoiceSettingFlg(); break;
				case DISP_SETTING_MENU_VS:    setting_voice_effect_en = !setting_voice_effect_en; Player_SetVoiceEffectFlg(setting_voice_effect_en); break;
				case DISP_SETTING_MENU_VTR:   setting_time_report_en = !setting_time_report_en; Player_SetVoiceTimeFlg(setting_time_report_en); break;
				case DISP_SETTING_MENU_NTP:   setting_ntp_en = !setting_ntp_en; Rtc_SetNtpEnStatus(setting_ntp_en); break;
				case DISP_SETTING_MENU_DATE:  
					/* 手动设置时间只能在NTP设置为OFF时才有效 */
					disp_flg = (0 == setting_ntp_en) ? (DISP_SETTING_DATE) : (DISP_SETTING_DATE_ERROR); 
					if (0 == setting_ntp_en)
						Rtc_UpdateSetFromCurTime();
					break;
				case DISP_SETTING_MENU_CITY:  disp_flg = DISP_SETTING_CITY; Disp_CityCodeSplit(Weather_GetCityCode()); break;
				case DISP_SETTING_MENU_EXIT:  disp_flg = DISP_MENU; break;
			}
			break;
		}
		case DISP_SETTING_ALARM:
		{
			switch(setting_alarm_flg)
			{
				case DISP_SETTING_ALARM_PREV: alarm_num = (alarm_num<=1)?(DISP_SETTING_ALARM_MAX):alarm_num-1; break;
				case DISP_SETTING_ALARM_ENTER: disp_flg = DISP_SETTING_ALARM_DETAIL; break;
				case DISP_SETTING_ALARM_NEXT: alarm_num = (alarm_num>=DISP_SETTING_ALARM_MAX)?(1):alarm_num+1; break;
				case DISP_SETTING_ALARM_EXIT: disp_flg =DISP_SETTING_MENU; break;
			}
			break;
		}
		case DISP_SETTING_ALARM_DETAIL:
		{
			switch(detail_setting_alarm_flg)
			{
				case DISP_SR_DETAIL_PREV: alarm_num = (alarm_num<=1)?(DISP_SETTING_ALARM_MAX):alarm_num-1; break;
				case DISP_SR_DETAIL_NEXT: alarm_num = (alarm_num>=DISP_SETTING_ALARM_MAX)?(1):alarm_num+1; break;
				case DISP_SR_DETAIL_HOUR: 
				{
					if(alarm_af_flg == 1)
					{
						alarm_af_flg = 0;
					}
					else
					{
						alarm_time[alarm_num-1].alarm_h = (alarm_time[alarm_num-1].alarm_h<=0)?23:alarm_time[alarm_num-1].alarm_h-1;
					}
					alarm_aj_t = 0;
					break;
				}
				case DISP_SR_DETAIL_MIN: 
				{
					if(alarm_af_flg == 1)
					{
						alarm_af_flg = 0;
					}
					else
					{
						alarm_time[alarm_num-1].alarm_m = (alarm_time[alarm_num-1].alarm_m<=0)?59:alarm_time[alarm_num-1].alarm_m-1; 
					}	
					alarm_aj_t = 0;	
					break;			
				}
				case DISP_SR_DETAIL_EN: alarm_time[alarm_num-1].alarm_en = !alarm_time[alarm_num-1].alarm_en; break;
				case DISP_SR_DETAIL_MON: alarm_time[alarm_num-1].alarm_wk_bit = (alarm_time[alarm_num-1].alarm_wk_bit&ALARM_MONDAY_EN)?
				                                                   (alarm_time[alarm_num-1].alarm_wk_bit&(~ALARM_MONDAY_EN)):(alarm_time[alarm_num-1].alarm_wk_bit|(ALARM_MONDAY_EN)); break;
				case DISP_SR_DETAIL_TUES: alarm_time[alarm_num-1].alarm_wk_bit = (alarm_time[alarm_num-1].alarm_wk_bit&ALARM_TUESDAY_EN)?
				                                                   (alarm_time[alarm_num-1].alarm_wk_bit&(~ALARM_TUESDAY_EN)):(alarm_time[alarm_num-1].alarm_wk_bit|(ALARM_TUESDAY_EN)); break;
				case DISP_SR_DETAIL_WED: alarm_time[alarm_num-1].alarm_wk_bit = (alarm_time[alarm_num-1].alarm_wk_bit&ALARM_WEDNESDAY_EN)?
				                                                   (alarm_time[alarm_num-1].alarm_wk_bit&(~ALARM_WEDNESDAY_EN)):(alarm_time[alarm_num-1].alarm_wk_bit|(ALARM_WEDNESDAY_EN)); break;
				case DISP_SR_DETAIL_THUR: alarm_time[alarm_num-1].alarm_wk_bit = (alarm_time[alarm_num-1].alarm_wk_bit&ALARM_THURSDAY_EN)?
				                                                   (alarm_time[alarm_num-1].alarm_wk_bit&(~ALARM_THURSDAY_EN)):(alarm_time[alarm_num-1].alarm_wk_bit|(ALARM_THURSDAY_EN)); break;
				case DISP_SR_DETAIL_FIR: alarm_time[alarm_num-1].alarm_wk_bit = (alarm_time[alarm_num-1].alarm_wk_bit&ALARM_FIRDAY_EN)?
				                                                   (alarm_time[alarm_num-1].alarm_wk_bit&(~ALARM_FIRDAY_EN)):(alarm_time[alarm_num-1].alarm_wk_bit|(ALARM_FIRDAY_EN)); break;
				case DISP_SR_DETAIL_SAT: alarm_time[alarm_num-1].alarm_wk_bit = (alarm_time[alarm_num-1].alarm_wk_bit&ALARM_SATURDAY_EN)?
				                                                   (alarm_time[alarm_num-1].alarm_wk_bit&(~ALARM_SATURDAY_EN)):(alarm_time[alarm_num-1].alarm_wk_bit|(ALARM_SATURDAY_EN)); break;
				case DISP_SR_DETAIL_SUN: alarm_time[alarm_num-1].alarm_wk_bit = (alarm_time[alarm_num-1].alarm_wk_bit&ALARM_SUNDAY_EN)?
				                                                   (alarm_time[alarm_num-1].alarm_wk_bit&(~ALARM_SUNDAY_EN)):(alarm_time[alarm_num-1].alarm_wk_bit|(ALARM_SUNDAY_EN)); break;
				case DISP_SR_DETAIL_EXIT: disp_flg = DISP_SETTING_ALARM; break;
			}
			Alarm_SetAlarmTime(alarm_num-1, alarm_time[alarm_num-1]);
			break;
		}
		case DISP_SETTING_DATE:
		{
			switch(date_setting_flg)
			{	 
				case DISP_DATE_SETTING_HOUR: date_setting_flg = DISP_DATE_SETTING_EXIT;break;
				case DISP_DATE_SETTING_MIN:  date_setting_flg = DISP_DATE_SETTING_HOUR;break;
				case DISP_DATE_SETTING_SEC:  date_setting_flg = DISP_DATE_SETTING_MIN;break;
				case DISP_DATE_SETTING_YEAR: date_setting_flg = DISP_DATE_SETTING_SEC;break;
				case DISP_DATE_SETTING_MON:  date_setting_flg = DISP_DATE_SETTING_YEAR;break;
				case DISP_DATE_SETTING_MDAY: date_setting_flg = DISP_DATE_SETTING_MON;break;
				case DISP_DATE_SETTING_EXIT: date_setting_flg = DISP_DATE_SETTING_MDAY;break;
			}
			break;
		}
		case DISP_WIFI:
		{
			if (wifi_setting_flag <= 0)
			{
				wifi_setting_flag = wifi_ssid_name_len+wifi_pwd_len+2;
			}
			else
			{
				wifi_setting_flag --; 
			}

			if ((0 == wifi_setting_flag) || ((wifi_ssid_name_len+1) == wifi_setting_flag))
			{
				/* Do Nothing */
			}
			else if (wifi_setting_flag <= wifi_ssid_name_len)
			{
				key_input_char[0] = wifi_ssid_name[wifi_setting_flag-1]&0x0F;
				key_input_char[1] = wifi_ssid_name[wifi_setting_flag-1]>>4;
			}
			else if (wifi_setting_flag <= (wifi_ssid_name_len+1+wifi_pwd_len))
			{
				key_input_char[0] = wifi_pwd[wifi_setting_flag-wifi_ssid_name_len-2]&0x0F;
				key_input_char[1] = wifi_pwd[wifi_setting_flag-wifi_ssid_name_len-2]>>4;
			}
			break;
		}
		case DISP_WEATHER:
		{
			disp_flg = DISP_MENU;
			break;
		}
		case DISP_SETTING_CITY:
		{
			setting_city_flag = (setting_city_flag <= DISP_CITY_CODE_1) ? (DISP_CITY_EXIT):(setting_city_flag-1);
			break;
		}
		default:
		{
			break;
		}
	}
}

/* 按键1按住保持逻辑 */
void Disp_Key1Holding(void)
{
	static uint8_t volume_increase_s = 0;

	switch(disp_flg)
	{
		case DISP_SETTING_MENU:
		{
			switch(setting_menu_flg)
			{		 
				case DISP_SETTING_MENU_VOLU:  
				{
					if(volume_increase_s > VOLUME_INCREASE_T)
					{
						volume_increase_s = 0;
						setting_volume = (setting_volume<100)?(setting_volume+1):(setting_volume); 
					}
					else
					{
						volume_increase_s ++;
					}
					break;
				}
				default:
				{

				}
			}
			break;
		}
		case DISP_SETTING_ALARM_DETAIL:
		{
			switch(detail_setting_alarm_flg)
			{
				case DISP_SR_DETAIL_HOUR: 
				{
					if(alarm_aj_t > DISP_SR_DETAIL_AD_THD)
					{
						alarm_af_flg = 1;
						alarm_aj_t = 0;
						alarm_time[alarm_num-1].alarm_h = (alarm_time[alarm_num-1].alarm_h>=23)?0:alarm_time[alarm_num-1].alarm_h+1; 
					}
					else
					{
						alarm_aj_t++;
					}
					break;
				}
				case DISP_SR_DETAIL_MIN: 
				{
					if(alarm_aj_t > DISP_SR_DETAIL_AD_THD)
					{
						alarm_af_flg = 1;
						alarm_aj_t = 0;
						alarm_time[alarm_num-1].alarm_m = (alarm_time[alarm_num-1].alarm_m>=59)?0:alarm_time[alarm_num-1].alarm_m+1;
					}
					else
					{
						alarm_aj_t++;
					}
					break;
				}
				default: break;
			}
			Alarm_SetAlarmTime(alarm_num-1, alarm_time[alarm_num-1]);
			break;
		}
		default:
		{

		}
	}
}

/* 按键2按住保持逻辑 */
void Disp_Key2Holding(void)
{
	static uint8_t volume_decrease_s = 0;

	switch(disp_flg)
	{
		case DISP_SETTING_MENU:
		{
			switch(setting_menu_flg)
			{		 
				case DISP_SETTING_MENU_VOLU:  
				{
					if(volume_decrease_s > VOLUME_INCREASE_T)
					{
						volume_decrease_s = 0;
						setting_volume = (setting_volume>0)?(setting_volume-1):(setting_volume); 
					}
					else
					{
						volume_decrease_s ++;
					}
					break;
				}
				default:
				{

				}
			}
			break;
		}
		case DISP_SETTING_ALARM_DETAIL:
		{
			switch(detail_setting_alarm_flg)
			{
				case DISP_SR_DETAIL_HOUR: 
				{
					if(alarm_aj_t > DISP_SR_DETAIL_AD_THD)
					{
						alarm_af_flg = 1;
						alarm_aj_t = 0;
						alarm_time[alarm_num-1].alarm_h = (alarm_time[alarm_num-1].alarm_h<=0)?23:alarm_time[alarm_num-1].alarm_h-1;
					}
					else
					{
						alarm_aj_t++;
					}
					break;
				}
				case DISP_SR_DETAIL_MIN: 
				{
					if(alarm_aj_t > DISP_SR_DETAIL_AD_THD)
					{
						alarm_af_flg = 1;
						alarm_aj_t = 0;
						alarm_time[alarm_num-1].alarm_m = (alarm_time[alarm_num-1].alarm_m<=0)?59:alarm_time[alarm_num-1].alarm_m-1;
					}
					else
					{
						alarm_aj_t++;
					}
					break;
				}
				default: break;
			}
			Alarm_SetAlarmTime(alarm_num-1, alarm_time[alarm_num-1]);
			break;
		}
		default:
		{

		}
	}
}

void Disp_SetSettingCityCode(uint32_t citycode)
{
	Disp_CityCodeSplit(citycode);
}

void Disp_SetSettingVolmue(int value)
{
	setting_volume = value;
}

void Disp_setSettingSoundEffect(int value)
{
	setting_voice_effect_en = value;
}

void Disp_SetSettingSoundTime(int value)
{
	setting_time_report_en = value;
}

void Disp_setSettingNtpEn(int value)
{
	setting_ntp_en = value;
}
